<?php
/**
 * 关键词回复设置
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/9 0009
 * Time: 15:33
 */
namespace application\admin\controller;
use think\Db;
use think\Session;

class ReplyController extends  CommonController{
    public function index(){
        $where['reply_type'] ='0';
        Session::get('user_id') != 1 ? $where['r.user_id'] = Session::get('user_id') : '';
        $lists = db("replyset")->alias('r')->join('__WECHATAPP__ w','r.app_id = w.app_id','left')->where($where)->order('reply_set_id desc')->paginate(10);
        //print_r($lists);
        $this->assign('lists', $lists);
        return $this->fetch();
    }
    /**
     * 编辑app配置
     */
    public function  info(){
        if(request()->isPost()){
            $id = $this->request->param('id');
            $map['reply_keywords'] = $this->request->param('reply_keywords');
            $map['reply_answer'] = $this->request->param('reply_answer');
            $map['reply_type'] = $this->request->param('reply_type');
            $map['app_id'] = $this->request->param('app_id');
            $result = Db::name('replyset')->where('reply_set_id',$id)->update($map);
            if ($result){
                $this->success('操作成功', url('index'));
            }
        }

        $id = $this->request->param('id');
        $data = Db::name('replyset')->where('reply_set_id',$id)->find();
        Session::get('user_id') != 1 ? $where['user_id'] = Session::get('user_id') : '';

        $app_name = Db::name('wechatapp')->where($where)->field('app_id,appname')->select();
        $this->assign('app_names',$app_name);
        $this->assign('info',$data);
        return $this->fetch();
    }
    //添加
    public function add(){
        $map['reply_keywords'] = $this->request->param('reply_keywords');
        $map['reply_answer'] = $this->request->param('reply_answer');
        $map['reply_type'] = $this->request->param('reply_type');
        $map['app_id'] = $this->request->param('app_id');
        $map['user_id'] = Session::get('user_id');
        $appid = $map['app_id'];
        $data = Db::name('wechatapp')->where('app_id',$appid)->find();
        if($data['app_type'] == '0'){
            $map2['reply_keywords'] = $map['reply_keywords'];
            $map2['app_id'] = $appid;
            $reply_result = Db::name('replyset')->where($map2)->find();
            if($reply_result){
                $this->error('操作失败 订阅号只能有一个相同的关键字', url('index'));
            }else{
                $map['addtime'] = date('Y-m-d H:i:s',time());
                $result = Db::name('replyset')->insert($map);
                if ($result){
                    $this->success('操作成功', url('index'));
                }
            }
        }else {
            $map['addtime'] = date('Y-m-d H:i:s', time());
            $result = Db::name('replyset')->insert($map);
            if ($result) {
                $this->success('操作成功', url('index'));
            }
        }
    }
    public function del(){
        $id = $this->request->param('id');
        $result = Db::name('replyset')->where('reply_set_id',$id)->delete();
        if ($result){
            $this->success('操作成功', url('index'));
        }
    }

    /**
     *  图文回复管理
     */
    public function textImage(){
        //$where['reply_type']='1';
        Session::get('user_id') != 1 ? $where['r.user_id'] = Session::get('user_id') : '';
        $lists = db("replyset")->alias('r')->join('__WECHATAPP__ w','r.app_id = w.app_id','left')->where($where)->order('reply_set_id desc')->paginate(10);
        //print_r($lists);
        $this->assign('lists', $lists);
        return $this->fetch();
    }
    public function  imageInfo(){
        if(request()->isPost()){
            $file = request()->file('image_url');
            if ($file) {
                $file_types = explode(".", $_FILES ['image_url'] ['name']); // ["name"] => string(25) "excel文件名.xls"
                $file_type = $file_types [count($file_types) - 1];//xls后缀
                /*判别是不是.xls文件，判别是不是excel文件*/
                if (strtolower($file_type) != "jpg" && strtolower($file_type) != "png") {
                    echo '不是图片文件，重新上传jpg或png文件';
                    die;
                }
                $info = $file->move(ROOT_PATH . 'public' .DS.'uploads'. DS . 'images');
                if($info){
                    $path=DS.'uploads'.DS .'images/'.$info->getSaveName();
                    $map['image_url'] = $path;
                }
            }

            $id = $this->request->param('id');
            $map['reply_keywords'] = $this->request->param('reply_keywords');
            $map['title'] = $this->request->param('title');

            $map['url'] = $this->request->param('url');
            $map['description'] = $this->request->param('description');
            $map['app_id'] = $this->request->param('app_id');
            $map['reply_type'] = '1';
            $result = Db::name('replyset')->where('reply_set_id',$id)->update($map);
            if ($result){
                $this->success('操作成功', url('textimage'));
            }
        }

        $id = $this->request->param('id');
        $data = Db::name('replyset')->where('reply_set_id',$id)->find();
        Session::get('user_id') != 1 ? $where['user_id'] = Session::get('user_id') : '';

        $app_name = Db::name('wechatapp')->where($where)->field('app_id,appname')->select();
        $this->assign('app_names',$app_name);
        $this->assign('info',$data);
        return $this->fetch();
    }
    public function imageInfou(){
        if(request()->isPost()){
            $file = request()->file('image_url');
            if ($file) {
                $file_types = explode(".", $_FILES ['image_url'] ['name']); // ["name"] => string(25) "excel文件名.xls"
                $file_type = $file_types [count($file_types) - 1];//xls后缀
                /*判别是不是.xls文件，判别是不是excel文件*/
                if (strtolower($file_type) != "jpg" && strtolower($file_type) != "png") {
                    echo '不是图片文件，重新上传jpg或png文件';
                    die;
                }
                $info = $file->move(ROOT_PATH . 'public' .DS.'uploads'. DS . 'images');
                if($info){
                    $path=DS.'uploads'.DS .'images/'.$info->getSaveName();
                    $map['image_url'] = $path;
                }
            }

            $id = $this->request->param('id');
            $map['reply_keywords'] = $this->request->param('reply_keywords');
            $map['title'] = $this->request->param('title');

            $map['url'] = $this->request->param('url');
            $map['description'] = $this->request->param('description');
            $map['app_id'] = $this->request->param('app_id');
            $map['reply_type'] = '1';
            $result = Db::name('replyset')->where('reply_set_id',$id)->update($map);
            if ($result){
                $this->success('操作成功', url('textimage'));
            }
        }

        $id = $this->request->param('id');
        $data = Db::name('replyset')->where('reply_set_id',$id)->find();
        Session::get('user_id') != 1 ? $where['user_id'] = Session::get('user_id') : '';

        $app_name = Db::name('wechatapp')->where($where)->field('app_id,appname')->select();
        $this->assign('app_names',$app_name);
        $this->assign('info',$data);
        return $this->fetch();
    }
    public function imageInfoAdd(){
        if(request()->isPost()) {
            //  print_r($_POST);
            $file = request()->file('image_url');


            $reply_keywords = $this->request->param('reply_keywords');
            $app_id = $this->request->param('app_id');
            $reply_type = $this->request->param('reply_type');
            $reply_answer = $_POST['reply_answer'];

            if ($reply_type == '0') {
                for ($i = 0; $i < count($reply_answer); $i++) {
                    $map['reply_keywords'] = $reply_keywords;
                    $map['app_id'] = $app_id;
                    $map['addtime'] = date('Y-m-d H:i:s', time());
                    $map['user_id'] = Session::get('user_id');
                    $map['reply_type'] = $reply_type;
                    $map['reply_answer'] = $reply_answer[$i];
                    $appid = $map['app_id'];
                    $data = Db::name('wechatapp')->where('app_id', $appid)->find();
                    if ($data['app_type'] == '0') {
                        $map2['reply_keywords'] = $map['reply_keywords'];
                        $map2['app_id'] = $appid;
                        $reply_result = Db::name('replyset')->where($map2)->find();
                        if ($reply_result) {
                            $this->error('操作失败 订阅号只能有一个相同的关键字', url('index'));
                        } else {
                            $result = Db::name('replyset')->insert($map);
                            // if ($result){
                            //  $this->success('操作成功', url('textImage'));
                            //  }
                        }
                    } else {
                        $result = Db::name('replyset')->insert($map);
                        // if ($result){
                        // $this->success('操作成功', url('textImage'));
                        //  }
                    }
                }
                $this->success('操作成功', url('textImage'));
            } else {

                $title = $_POST['title'];
                $url = $_POST['url'];
                $description = $_POST['description'];

                //  $map['image_url'] = $path ? $path=$path : '';

                for ($i = 0; $i < count($title); $i++) {
                    if ($file[$i]) {
                        //  print_r($_FILES);
                        $file_types = explode(".", $_FILES ['image_url'] ['name'][$i]); // ["name"] => string(25) "excel文件名.xls"
                        //    print_r($file_types);
                        // die();
                        $file_type = $file_types [count($file_types) - 1];//xls后缀
                        /*判别是不是.xls文件，判别是不是excel文件*/
                        if (strtolower($file_type) != "jpg" && strtolower($file_type) != "png") {
                            echo '不是图片文件，重新上传jpg或png文件';
                            die;
                        }
                        $info = $file[$i]->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'images');
                        if ($info) {
                            $path = DS . 'uploads' . DS . 'images/' . $info->getSaveName();

                        }
                    }

                    //   echo $path;
                    $map['reply_keywords'] = $reply_keywords;
                    $map['title'] = $title[$i];
                    $map['image_url'] = $path ? $path = $path : '';
                    $map['url'] = $url[$i];
                    $map['description'] = $description[$i];
                    $map['reply_type'] = '1';
                    $map['addtime'] = date('Y-m-d H:i:s', time());
                    $map['user_id'] = Session::get('user_id');
                    $map['app_id'] = $app_id;
                    //  echo $title[$i];
                    //  echo  $url[$i];
                    $appid = $map['app_id'];
                    $data = Db::name('wechatapp')->where('app_id', $appid)->find();
                    if ($data['app_type'] == '0') {
                        $map2['reply_keywords'] = $map['reply_keywords'];
                        $map2['app_id'] = $appid;
                        $reply_result = Db::name('replyset')->where($map2)->find();
                        if ($reply_result) {
                            $this->error('操作失败 订阅号只能有一个相同的关键字', url('index'));
                        } else {
                            $result = Db::name('replyset')->insert($map);
                            // if ($result){
                            //  $this->success('操作成功', url('textImage'));
                            //  }
                        }
                    } else {
                        $result = Db::name('replyset')->insert($map);
                        // if ($result){
                        // $this->success('操作成功', url('textImage'));
                        //  }
                    }
                }

                $this->success('操作成功', url('textImage'));

            }
        }
    }
    public function imagedel(){
        $id = $this->request->param('id');
        $result = Db::name('replyset')->where('reply_set_id',$id)->delete();
        if ($result){
            $this->success('操作成功', url('textimage'));
        }
    }
}